using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using FridayShopping.DataModel;

namespace FridayShopping.UI
{
	/// <summary>
	/// Summary description for ShopRoute.
	/// </summary>
	public class ShopRoute : System.Windows.Forms.Form, IListViewForm
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.ToolBar toolBar;
		private System.Windows.Forms.ImageList imageList;
		private System.Windows.Forms.ToolBarButton toolBarButton_Up;
		private System.Windows.Forms.ToolBarButton toolBarButton_Down;
		private System.Windows.Forms.ListView listView;
		private System.Windows.Forms.ColumnHeader columnHeader_Key;
		private System.Windows.Forms.ColumnHeader columnHeader_CategoryName;
			
		private Shop			shop;
		private ListFormManager	listFormManager;
		private ArrayList		categorieKeys;

		public ShopRoute(Shop shop)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			toolBar.ImageList = imageList;
			toolBar.Buttons[0].ImageIndex = 0;
			toolBar.Buttons[1].ImageIndex = 1;

			this.shop = shop;

			// Prepare the categories list from list of keys.
			
			ArrayList keys = shop.Categories;

			Categories categories = new Categories();
			foreach (Key key in keys) 
			{
				Category category = new Category();
				Category cat = (Category)Database.Instance.Categories.GetItem(key);
				if (cat != null) 
				{
					category.Name = cat.Name;
					category.Key = cat.Key;
					categories.AddWithKey(category);
				}
			}

			listFormManager = new ShopRouteListFormManager(this, listView, categories);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ShopRoute));
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.toolBar = new System.Windows.Forms.ToolBar();
			this.toolBarButton_Up = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton_Down = new System.Windows.Forms.ToolBarButton();
			this.imageList = new System.Windows.Forms.ImageList();
			this.listView = new System.Windows.Forms.ListView();
			this.columnHeader_Key = new System.Windows.Forms.ColumnHeader();
			this.columnHeader_CategoryName = new System.Windows.Forms.ColumnHeader();
			// 
			// toolBar
			// 
			this.toolBar.Buttons.Add(this.toolBarButton_Up);
			this.toolBar.Buttons.Add(this.toolBarButton_Down);
			this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
			// 
			// toolBarButton_Up
			// 
			this.toolBarButton_Up.Enabled = ((bool)(resources.GetObject("toolBarButton_Up.Enabled")));
			this.toolBarButton_Up.ImageIndex = ((int)(resources.GetObject("toolBarButton_Up.ImageIndex")));
			this.toolBarButton_Up.Visible = ((bool)(resources.GetObject("toolBarButton_Up.Visible")));
			// 
			// toolBarButton_Down
			// 
			this.toolBarButton_Down.Enabled = ((bool)(resources.GetObject("toolBarButton_Down.Enabled")));
			this.toolBarButton_Down.ImageIndex = ((int)(resources.GetObject("toolBarButton_Down.ImageIndex")));
			this.toolBarButton_Down.Visible = ((bool)(resources.GetObject("toolBarButton_Down.Visible")));
			// 
			// imageList
			// 
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource"))));
			this.imageList.Images.Add(((System.Drawing.Image)(resources.GetObject("resource1"))));
			this.imageList.ImageSize = ((System.Drawing.Size)(resources.GetObject("imageList.ImageSize")));
			// 
			// listView
			// 
			this.listView.Columns.Add(this.columnHeader_Key);
			this.listView.Columns.Add(this.columnHeader_CategoryName);
			this.listView.Enabled = ((bool)(resources.GetObject("listView.Enabled")));
			this.listView.FullRowSelect = true;
			this.listView.Location = ((System.Drawing.Point)(resources.GetObject("listView.Location")));
			this.listView.Size = ((System.Drawing.Size)(resources.GetObject("listView.Size")));
			this.listView.Text = resources.GetString("listView.Text");
			this.listView.View = System.Windows.Forms.View.Details;
			this.listView.Visible = ((bool)(resources.GetObject("listView.Visible")));
			this.listView.SelectedIndexChanged += new System.EventHandler(this.listView_SelectedIndexChanged);
			// 
			// columnHeader_Key
			// 
			this.columnHeader_Key.Text = resources.GetString("columnHeader_Key.Text");
			this.columnHeader_Key.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_Key.TextAlign")));
			this.columnHeader_Key.Width = ((int)(resources.GetObject("columnHeader_Key.Width")));
			// 
			// columnHeader_CategoryName
			// 
			this.columnHeader_CategoryName.Text = resources.GetString("columnHeader_CategoryName.Text");
			this.columnHeader_CategoryName.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("columnHeader_CategoryName.TextAlign")));
			this.columnHeader_CategoryName.Width = ((int)(resources.GetObject("columnHeader_CategoryName.Width")));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			// 
			// ShopRoute
			// 
			this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));
			this.Controls.Add(this.listView);
			this.Controls.Add(this.toolBar);
			this.Enabled = ((bool)(resources.GetObject("$this.Enabled")));
			this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Text = resources.GetString("$this.Text");
			this.Visible = ((bool)(resources.GetObject("$this.Visible")));
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ShopRoute_Closing);
			this.Load += new System.EventHandler(this.ShopRoute_Load);

		}
		#endregion

		#region Access Members
		public ArrayList CategoryKeys 
		{
			get
			{
				return categorieKeys;
			}
		}
		#endregion

		#region IListViewForm implementation
		public ToolBarButton DeleteButton
		{
			get
			{
				return null;
			}
		}

		public ToolBarButton NewButton
		{
			get
			{
				return null;
			}
		}

		public ToolBarButton EditButton
		{
			get
			{
				return null;
			}
		}

		public ToolBarButton UpButton
		{
			get
			{
				return toolBarButton_Up;
			}
		}

		public ToolBarButton DownButton
		{
			get
			{
				return toolBarButton_Down;
			}
		}

		public IItemPropertiesForm CreatePropertiesForm(ItemBase item)
		{
			return null;
		}

		#endregion

		#region Form Event Handlers

		private void ShopRoute_Load(object sender, System.EventArgs e)
		{
			listFormManager.FillListView();

			listFormManager.EnableDisableItems();		
		}

		private void toolBar_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			listFormManager.ToolbarEventsHandler(e);		
		}

		private void listView_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			listFormManager.EnableDisableItems();		
		}

		private void ShopRoute_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			ArrayList keys = new ArrayList();

			foreach (ListViewItem item in listView.Items)
			{
				Key key = listFormManager.GetItemKey(item);
				keys.Add(key);
			}
			categorieKeys = keys;		
		}
		#endregion

	}
}
